#coding=utf8

from helper import res, StandardList

from rest_framework import mixins
from rest_framework import generics
class Mylist(mixins.ListModelMixin,
                  generics.GenericAPIView):
    def get(self, request, *args, **kwargs):
        resp = self.list(request, *args, **kwargs)
        return res(0, "list", resp.data)

class Mydetail(mixins.RetrieveModelMixin,
                  generics.GenericAPIView):
    def get(self, request, *args, **kwargs):
        resp = self.retrieve(request, *args, **kwargs)
        return res(0, "retrieve", resp.data)

class MyList(mixins.ListModelMixin,
                  mixins.CreateModelMixin,
                  generics.GenericAPIView):

    def get(self, request, *args, **kwargs):
        resp = self.list(request, *args, **kwargs)
        return res(0, "list", resp.data)

    def post(self, request, *args, **kwargs):
        resp = self.create(request, *args, **kwargs)
        return res(0, "create", resp.data)

class MyDetail(mixins.RetrieveModelMixin,
                    mixins.UpdateModelMixin,
                    mixins.DestroyModelMixin,
                    generics.GenericAPIView):

    def get(self, request, *args, **kwargs):
        resp = self.retrieve(request, *args, **kwargs)
        return res(0, "retrieve", resp.data)

    def put(self, request, *args, **kwargs):
        resp = self.update(request, *args, **kwargs)
        return res(0, "update", resp.data)

    def delete(self, request, *args, **kwargs):
        resp = self.destroy(request, *args, **kwargs)
        return res(0, "delete", resp.data)


from rest_framework import viewsets
from django.shortcuts import get_object_or_404
class MyModelViewSet(viewsets.ViewSet):
    """
    Example empty viewset demonstrating the standard
    actions that will be handled by a router class.

    If you're using format suffixes, make sure to also include
    the `format=None` keyword argument for each action.
    """
    queryset = None
    serializer_class = None

    def get_queryset(self):
        return None

    def fetch_queryset(self):
        q = self.get_queryset()
        if q != None:
            return q
        else:
            return self.__class__().queryset

    def fetch_serializer_class(self):
        return self.__class__().serializer_class

    def list(self, request):
        data = StandardList(self.request, self.fetch_serializer_class(), self.fetch_queryset())
        return res(0, 'list', data)

    def perform_create(self, serializer):
        serializer.save()

    def create(self, request):
        serializer = self.fetch_serializer_class()(data=request.data)
        if serializer.is_valid():
            try:
                self.perform_create(serializer)
            except Exception, e:
                return res(-1, str(e))
            return res(0, 'create', serializer.data)
        return res(-1, 'create error!', serializer.errors)

    def retrieve(self, request, pk=None):
        obj = get_object_or_404(self.fetch_queryset(), pk=pk)
        serializer = self.fetch_serializer_class()(obj)
        return res(0, 'retrieve', serializer.data)

    def before_update(self, obj):
        pass

    def perform_update(self, serializer):
        serializer.save()

    def update(self, request, pk = None):
        obj = get_object_or_404(self.fetch_queryset(), pk=pk)
        serializer = self.fetch_serializer_class()(obj, data=request.data)
        if serializer.is_valid():
            try:
                self.before_update(obj)
                self.perform_update(serializer)
            except Exception, e:
                return res(-1, str(e))
            return res(0, 'update', serializer.data)
        return res(-1, 'update error!', serializer.errors)

    def perform_destroy(self, instance):
        instance.delete()

    def destroy(self, request, pk = None):
        obj = get_object_or_404(self.fetch_queryset(), pk=pk)
        try:
            self.perform_destroy(obj)
        except Exception, e:
            return res(-1, str(e))
        return res(0, 'delete')



class MyReadOnlyModelViewSet(viewsets.ViewSet):
    """
    Example empty viewset demonstrating the standard
    actions that will be handled by a router class.

    If you're using format suffixes, make sure to also include
    the `format=None` keyword argument for each action.
    """
    queryset = None
    serializer_class = None

    def get_queryset(self):
        return None

    def fetch_queryset(self):
        q = self.get_queryset()
        if q != None:
            return q
        else:
            return self.__class__().queryset

    def fetch_serializer_class(self):
        return self.__class__().serializer_class

    def list(self, request):
        data = StandardList(self.request, self.fetch_serializer_class(), self.fetch_queryset())
        return res(0, 'list', data)

    def retrieve(self, request, pk=None):
        obj = get_object_or_404(self.fetch_queryset(), pk=pk)
        serializer = self.fetch_serializer_class()(obj)
        return res(0, 'retrieve', serializer.data)

    def create(self, request):
        return res(-1, 'permission denied!')

    def update(self, request, pk = None):
        return res(-1, 'permission denied!')

    def destroy(self, request, pk = None):
        return res(-1, 'permission denied!')

